package com.zhihao.takeaway.controller;

import com.zhihao.takeaway.common.utils.EasyUITable;
import com.zhihao.takeaway.common.utils.JwtUtil;
import com.zhihao.takeaway.common.utils.Result;
import com.zhihao.takeaway.entity.BasePerson;
import com.zhihao.takeaway.service.ChatService;
import com.zhihao.takeaway.service.EmployeeService;
import com.zhihao.takeaway.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/toChat")
public class ChatController {

    @Autowired
    private ChatService chatService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserService userService;
    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/getBeforeRecord")
    public Result getBeforeEncord(String fromId,String toId,Integer fromSign,Integer toSign) {
        Result result = new Result();

        Boolean flag = judgeState(fromSign);
        if(!flag)
        {
            result.fail("您当前处于未登录状态，请及时登录，获取信息");
            return result;
        }
        BasePerson fromInfo = new BasePerson();
        BasePerson toInfo = new BasePerson();
        // sign --- 0 顾客 ----1 员工
        if(fromSign == 0) {
            fromInfo = userService.getById(fromId);
        }else if(fromSign == 1){
            fromInfo = employeeService.getById(fromId);
        }
        if(toSign == 0){
            toInfo = userService.getById(toId);
        }else if(toSign == 1){
            toInfo = employeeService.getById(toId);
        }
        EasyUITable easyUITable = chatService.selectEncord("record"+fromInfo.getAccount(),fromInfo.getAccount(),toInfo.getAccount());
        easyUITable.put("fromInfo",(BasePerson)fromInfo);
        easyUITable.put("toInfo",(BasePerson)toInfo);
        result.setData(easyUITable);
        return result;
    }
    @PostMapping("/saveRecord")
    public Result saveEncord(String fromAccount,String toAccount,String message){
        Result result = new Result();
        Boolean flag = judgeAccount(fromAccount);
        if(flag == false)
        {
            result.fail("您当前处于未登录状态，请及时登录，获取信息");
            return result;
        }
        chatService.saveEncord(fromAccount,toAccount,message);
        result.success("保存成功");
        return result;
    }

    public String getEmployeeId(){
        String token = request.getHeader("token");
        String EmployeeId = JwtUtil.validateToken(token);
        return EmployeeId;
    }
    public String getUserId(){
        String token = request.getHeader("token");
        String UserId = JwtUtil.validateToken(token);
        return UserId;
    }
    public Boolean judgeState(Integer sign){
        if(sign == 1){
            String EmployeeId  = getEmployeeId();
            if(EmployeeId == null || EmployeeId.equals("")) {
                return false;
            }
            // 用户获取聊天记录
        }else{
            String UserId = getUserId();
            if(UserId == null || UserId.equals("")) {
                return false;
            }
        }
        return true;
    }
    public Boolean judgeAccount(String fromAccount){
        if(fromAccount.substring(0,2).equals("YG")){
            String EmployeeId  = getEmployeeId();
            if(EmployeeId == null || EmployeeId.equals("")) {
                return false;
            }
            // 用户获取聊天记录
        }else{
            String UserId = getUserId();
            if(UserId == null || UserId.equals("")) {
                return false;
            }
        }
        return true;
    }
}
